package com.hsmpay.mobile.dao.finance.impl;

import com.hsmpay.common.dao.Impl.BaseDaoSupport;
import com.hsmpay.mobile.dao.finance.BatchClearLogDao;
import com.hsmpay.mobile.pojo.finance.BatchClearLog;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 批量结算日志dao 接口
 * @author 颜铃璋
 * 创建时间: 2013-6-3
 * 对应的pojo 是 BatchClear
 */
@Repository("batchClearLogDao")
public class BatchClearLogDaoImpl extends BaseDaoSupport<BatchClearLog,Long> implements BatchClearLogDao<BatchClearLog,Long> {
	/**
	 * 根据 文件ID和索引字段  查询日志信息
	 * @param fileId
	 * @param sort
	 * @return
	 * @throws SQLException
	 */
	public BatchClearLog searchBatchClearLog(Long fileId, Integer sort)throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setBcFileId(fileId);
		batchClearLog.setSort(sort);
		return searchEntity(batchClearLog);
	}
	
	/**
	 * 根据 文件ID和索引字段  查询日志信息
	 * @param fileId
	 * @param combineOrderId
	 * @return
	 * @throws SQLException
	 */
	public BatchClearLog searchBatchClearLog(Long fileId, Long combineOrderId)throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setBcFileId(fileId);
		batchClearLog.setCombineOrderId(combineOrderId);
		return searchEntity(batchClearLog);
	}
	
	/**
	 * 根据 文件ID和索引字段  查询日志信息
	 * @param fileId
	 * @param id  可能为空
	 * @param sort 可能为空
	 * @return
	 * @throws SQLException
	 */
	public BatchClearLog searchBatchClearLogByIdFId(Long fileId, Long id,Integer sort)throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setBcFileId(fileId);
		batchClearLog.setId(id);
		batchClearLog.setSort(sort);
		return (BatchClearLog)getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.searchBatchClearLogByIdFId", batchClearLog);
	}
	
	/** 
	 * 根据fileID 查询结算日志列表
	 * @return
	 * @throws SQLException
	 */
	public List<BatchClearLog> searchBatchClearLogListByFileId(Long fileId)throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setBcFileId(fileId);
		return searchEntityList(batchClearLog);
	}
	
	/** 
	 * 根据fileID 查询 佣金结算日志列表
	 * @return
	 * @throws SQLException
	 */
	public List<BatchClearLog> searchBrokBCLListByFileId(Long fileId)throws SQLException {
		BatchClearLog bcl = new BatchClearLog();
		bcl.setBcFileId(fileId);
		return getBrokerageLogs(bcl);
	}
	
	/**
	 * 根据文件ID 和 状态 查询列表
	 * @param fileId
	 * @param status
	 * @return
	 * @throws SQLException
	 */
	public List<BatchClearLog> searchBCLListByFileIdAndStatus(Long fileId,Integer status)throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setBcFileId(fileId);
		batchClearLog.setStatus(status);
		return searchEntityList(batchClearLog);
	}

	/**
	 * 获取佣金日志列表
	 * @param bcl
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List<BatchClearLog> getBrokerageLogs(BatchClearLog bcl) throws SQLException{
		return getSqlMapClientTemplate().queryForList("batchClearLogSqlMap.getBrokerageLogs", bcl);
	}

	/**
	 * 根据结算文件记录ID批量更新合单日志状态
	 * @param status
	 * @param reqCode
	 * @param reqMsg
	 * @return
	 * @throws SQLException
	 */
	public int updateByBatchClearFileId(Long bclId, int status,String reqCode,String reqMsg) throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setBcFileId(bclId);
		batchClearLog.setStatus(status);
		batchClearLog.setResponseCode(reqCode);
		batchClearLog.setMessage(reqMsg);
		return getSqlMapClientTemplate().update("batchClearLogSqlMap.updateByBatchClearFileId", batchClearLog);
	}
	
	/**
	 * 根据结算文件记录ID批量更新合单日志状态
	 * @param id
	 * @param isReturnAB
	 * @return
	 * @throws SQLException
	 */
	public int updateByBCLIdAndIsReturnAB(Long id, int isReturnAB) throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setId(id);
		batchClearLog.setIsReturnAB(isReturnAB);
		return getSqlMapClientTemplate().update("batchClearLogSqlMap.updateByBCLIdAndIsReturnAB", batchClearLog);
	}
	
	/**
	 * 获取成功 手续费费和
	 * @param bcFileId
	 * @return
	 * @throws SQLException
	 */
	public double getSuccRevenueFeeSumByFileId(Long bcFileId) throws SQLException{
		Double sum = (Double) getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.getSuccRevenueFeeSumByFileId", bcFileId);
		return null == sum ? 0.00 : sum;
	}
	
	/**
	 * 根据 ids 查询  需要回滚 的用户的结算日志
	 * @param ids
	 * @return
	 * @throws SQLException
	 */
	public List<BatchClearLog> searchRollbackUserMoneyBatchClearLogListByIds(String ids) throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setOtherParam(" BCL.STATUS=0 AND BCL.ID IN ("+ids+")");
		return searchEntityList(batchClearLog);
	}
	
	/**
	 * 根据ids 获取是否回滚过的用户
	 * @param ids
	 * @return
	 * @throws SQLException
	 */
	public int getIsReturnABCountByIds(String ids)throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setIsReturnAB(1);
		batchClearLog.setOtherParam(" BCL.STATUS=0 AND BCL.ID IN ("+ids+")");
		return getEntityCount(batchClearLog);
	}
	
	/**
	 * 根据bcId 查询错误订单且 没有 退回的订单数   即没有完成结算的订单数
	 * @param bcId
	 * @return
	 * @throws SQLException
	 */
	public int getUnfinishLogCountByBcId(Long bcId) throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setStatus(0);
		batchClearLog.setIsReturnAB(0);
		batchClearLog.setBatchClearId(bcId);
		return getEntityCount(batchClearLog);
	}
	
	/**
	 * 根据bcId 查询错误订单数
	 * @param bcId
	 * @return
	 * @throws SQLException
	 */
	public int getErrorLogCountByBcId(Long bcId) throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setStatus(0);
		batchClearLog.setBatchClearId(bcId);
		return getEntityCount(batchClearLog);
	}
	
	/**
	 * 根据bcId 查询错误订单 交易总额
	 * @param bcId
	 * @return
	 * @throws SQLException
	 */
	public double getErrorLogSumMoneyByBcId(Long bcId) throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setStatus(0);
		batchClearLog.setBatchClearId(bcId);
		Double sum = (Double) getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.getSumMoneyByBcId", batchClearLog);
		return null == sum ? 0.00 : sum;
	}
	
	/**
	 * 修改通道
	 * @param id
	 * @param channelId
	 * @param accBankCode
	 * @param revenueFee
	 * @return
	 * @throws SQLException
	 */
	public int updateBCLChannelById(Long id,Long channelId,String accBankCode,Double revenueFee) throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setId(id);
		batchClearLog.setChannelId(channelId);
		batchClearLog.setAccBankCode(accBankCode);
		batchClearLog.setRevenueFee(revenueFee);
		return updateEntity(batchClearLog);
	}
	
	/**
	 * 根据合单ID 修改日志  
	 * @param combineOrderId
	 * @param status
	 * @return
	 * @throws SQLException
	 */
	public int updateLogByCombineOrderId(Long combineOrderId,Integer status,Integer clearType)throws SQLException{
		BatchClearLog batchClearLog = new BatchClearLog();
		batchClearLog.setCombineOrderId(combineOrderId);
		batchClearLog.setStatus(status);
		batchClearLog.setClearType(clearType);
		return getSqlMapClientTemplate().update("batchClearLogSqlMap.updateLogByCombineOrderId", batchClearLog);
	}

	/**
	 * 查询金额和笔数
	 * @param batchClearLog
	 * @return
	 * @throws SQLException
	 */
	public BatchClearLog getTransMoney(BatchClearLog batchClearLog) throws SQLException {
		return (BatchClearLog) getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.getTransMoney", batchClearLog);
	}

	/**
	 * 查看结算日志详情
	 * @param batchClearLog
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List<BatchClearLog> searchLogDetialsList(BatchClearLog batchClearLog) throws SQLException {
		return getSqlMapClientTemplate().queryForList("batchClearLogSqlMap.searchLogDetialsList", batchClearLog);
	}

	/**
	 * 结算日志详情统计数
	 * @param batchClearLog
	 * @return
	 * @throws SQLException
	 */
	public int getLogDetailsCount(BatchClearLog batchClearLog) throws SQLException {
		return (Integer) getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.getLogDetailsCount", batchClearLog);
	}

	/**
	 * 查询金额和笔数
	 * @param batchClearLog
	 * @return
	 * @throws SQLException
	 */
	public BatchClearLog getTransMoneyByIds(BatchClearLog batchClearLog) throws SQLException {
		return (BatchClearLog) getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.getTransMoneyByIds", batchClearLog);
	}
	
	/**
	 * 根据ids 批量删除日志
	 * @param ids
	 * @throws SQLException
	 */
	public void deleteEntityByIds(String ids)throws SQLException{
		getSqlMapClientTemplate().delete("batchClearLogSqlMap.deleteEntityByIds", ids);
	}
	
	/**
	 * 单表计算
	 * @param bcl
	 * @return
	 * @throws SQLException
	 */
	public int getEntityCount_only(BatchClearLog bcl)throws SQLException{
		return (Integer)getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.getEntityCount_only", bcl);
	}
	
	/**
	 * 根据 日志ID 修改日志 sort
	 * @param id
	 * @param sort
	 * @throws SQLException
	 */
	public int updateBCLSortById(Long id, int sort)throws SQLException{
		BatchClearLog bcl = new BatchClearLog();
		bcl.setId(id);
		bcl.setSort(sort);
		return updateEntity(bcl);
	}
	/**
	 * 出金系统数据格式 
	 */
	@Override
	public List<Map> selectByBatchclearId(Map<String, Object> parameterMap) {
		return getSqlMapClientTemplate().queryForList("batchClearLogSqlMap.selectByBatchclearId",parameterMap);
	}

	@Override
	public int updateStatusByBatchClearId(Long batchClearId, Integer status) {
		Map<String, Object> parameterMap = new HashMap<String,Object>();
		parameterMap.put("batchClearId", batchClearId);
		parameterMap.put("status", status);
		return getSqlMapClientTemplate().update("batchClearLogSqlMap.updateStatusByBatchClearId",parameterMap);
	}

	@Override
	public int countByBcFileIdByStatus(Long clearFileId, int status) {
		Map<String, Object> parameterMap = new HashMap<String,Object>();
		parameterMap.put("clearFileId", clearFileId);
		parameterMap.put("status", status);
		return (Integer)getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.countByBcFileIdByStatus",parameterMap);
	}
	@Override
	public int countBankAvailable(String code) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("code", code);
		return (Integer) getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.countBankAvailable",code);
	}

	@Override
	public List<Map<String, Object>> selectNotInZxbackCodeByBcfIds(Map<String, Object> parameterMap) {
		return getSqlMapClientTemplate().queryForList("batchClearLogSqlMap.selectNotInZxbackCodeByBcfIds",parameterMap);
	}

	@Override
	public String selectBankNameByZxbackCode(String code) {
		return (String) getSqlMapClientTemplate().queryForObject("batchClearLogSqlMap.selectBankNameByZxbackCode",code);
	}
}
